package com.leo.effects;

import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.dsl.components.Effect;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.texture.AnimatedTexture;
import com.almasb.fxgl.texture.AnimationChannel;
import javafx.util.Duration;
import org.jetbrains.annotations.NotNull;

public class HelmetEffect extends Effect {

    private AnimatedTexture at;

    public HelmetEffect(@NotNull Duration duration) {
        super(duration);
        AnimationChannel ac = new AnimationChannel(FXGL.image("item/armed_helmet.png"), Duration.seconds(0.5), 4);
        at = new AnimatedTexture(ac);
    }

    /**
     * 效果结束的时候
     *
     * @param entity
     */
    @Override
    public void onEnd(@NotNull Entity entity) {
        at.stop();
        entity.getViewComponent().removeChild(at);
    }

    /**
     * 效果开始的时候
     *
     * @param entity
     */
    @Override
    public void onStart(@NotNull Entity entity) {
        at.setTranslateX(entity.getWidth() / 2.0 - at.getWidth() / 4.0 / 2.0);
        at.setTranslateY(entity.getHeight() / 2.0 - at.getHeight() / 2.0);
        at.loop();
        entity.getViewComponent().addChild(at);
    }
}
